// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

import 'unit.dart';


const _padding = EdgeInsets.all(16.0);
/// Converter screen where users can input amounts to convert.
///
/// Currently, it just displays a list of mock units.
///
/// While it is named ConverterRoute, a more apt name would be ConverterScreen,
/// because it is responsible for the UI at the route's destination.
class ConverterRoute extends StatefulWidget {
   /// Color for this [Category].
  final Color color;

  /// Units for this [Category].
  final List<Unit> units;

  const ConverterRoute({
    @required this.color,
    @required this.units,
  })  : assert(color != null),
        assert(units != null);

  @override
  _ConverterRouteState createState() => _ConverterRouteState();
}

class _ConverterRouteState extends State<ConverterRoute> {
  // TODO: Set some variables, such as for keeping track of the user's input
  // value and units

  // TODO: Determine whether you need to override anything, such as initState()

  // TODO: Add other helper functions. We've given you one, _format()

  /// Clean up conversion; trim trailing zeros, e.g. 5.500 -> 5.5, 10.0 -> 10
  String _format(double conversion) {
    var outputNum = conversion.toStringAsPrecision(7);
    if (outputNum.contains('.') && outputNum.endsWith('0')) {
      var i = outputNum.length - 1;
      while (outputNum[i] == '0') {
        i -= 1;
      }
      outputNum = outputNum.substring(0, i + 1);
    }
    if (outputNum.endsWith('.')) {
      return outputNum.substring(0, outputNum.length - 1);
    }
    return outputNum;
  }

  // String _showVaildationError(val) {
  //   return val;
  // }
  
  @override
  Widget build(BuildContext context) {
    // includes the input value, and 'from' unit [Dropdown].
    final input = Padding(
      padding: _padding,
      child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Directionality(
              textDirection: TextDirection.rtl,
              child: TextField(
                keyboardType: TextInputType.number,
                style: Theme.of(context).textTheme.display1,
                decoration: InputDecoration(
                  labelText: 'input',
                  labelStyle: Theme.of(context).textTheme.display1,
                  // errorText: _showVaildationError ? 'Invalid number entered' : null,
                ),
                onChanged: (String value) => {
                  print(value)
                },
              ),
            )
          ],
        ),
    );

    IconData compareArrows = Icons.compare_arrows;

    // final arrowsIcon = Icon(
    //   compareArrows,
    //   size: 30.0,
    //   textDirection: TextDirection.ltr,
    // );
    final arrowsIcon = InkWell(
      child: Container(
          child: Icon(
            compareArrows,
            size: 30.0,
          ),
          transform: Matrix4.identity()..rotateZ(90.0),
      )
      
    );
    
    // TODO: Create the 'output' group of widgets. This is a Column that
    // includes the output value, and 'to' unit [Dropdown].
    final unitWidgets = Container(
        color: widget.color,
        margin: EdgeInsets.all(8.0),
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: [
            input,
            arrowsIcon,
          ],  
        ),
      );
    // TODO: Return the input, arrows, and output widgets, wrapped in a Column.


    return unitWidgets;
  }
}